var USER_EDIT_TPL = `<div>
<el-dialog :title="title" :visible.sync="visible" width="50%" :before-close="handleClose">
	<el-form label-width="80px">
		<el-form-item label="编号" v-if="mode=='edit'">
			<el-input v-model="item.user_code" :disabled="true"></el-input>
		</el-form-item>
		<el-form-item label="姓名">
			<el-input v-model="item.name"></el-input>
		</el-form-item>
		<el-form-item label="昵称">
			<el-input v-model="item.nickname"></el-input>
		</el-form-item>
		<el-form-item label="年龄">
			<el-input-number v-model="item.age"></el-input-number>
		</el-form-item>
		<el-form-item label="金额">
			<el-input v-model="item.amount"></el-input>
		</el-form-item>
		<el-form-item label="会员">
			<el-switch v-model="item.is_vip" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
		</el-form-item>
	</el-form>
	<span slot="footer" class="dialog-footer">
		<el-button @click="visible = false">取 消</el-button>
		<el-button type="primary" @click="exec">确 定</el-button>
	</span>
</el-dialog>
</div>`;

Vue.component('UserEditComponent', {
    template: USER_EDIT_TPL,
	data: function(){
    	return {
			parent: null,
			title: '添加用户',
    		data: [],
			mode: 'add',
			item: {},
			visible: false,
    	}
    },
    mounted: function(){},
	methods: {
		init: function(app){ this.parent = app },
		edit: function(user_code) {
			this.mode = 'edit'
			this.title = '编辑用户'
			var that = this;
			var uri = '/user/profile'
			axios.get(uri, {params: {user_code: user_code}}).then(function (res) {
            	that.item = res.data.data
				that.visible = true
			}).catch(function (error) {
				console.error(error);
			}).then(function () {});
		},
		add: function() {
			this.mode = 'add'
			this.title = '添加用户'
			this.item = {
				name: "",
				nickname: "",
				age: 24,
				amount: "0",
				is_vip: false,
			}
			this.visible = true
		},
	   	handleClose(done) {
	        this.$confirm('确认关闭？').then(_ => { done(); }).catch(_ => {});
       	},
		exec(){
			if(this.mode == 'edit'){
				this.doEdit()
			} else if (this.mode == 'add') {
				this.doAdd()
			}
		},
		doEdit(){
			var that = this;
			var user_condition = this.parent.$refs.user_condition
			var uri = '/user/update'
			axios.post(uri, that.item).then(function (res) {
				console.log('res', res)
				if(user_condition){
					user_condition.search()
				}
				that.visible = false
			}).catch(function (error) {
				if(error.response && error.response.data && error.response.data.message){
					that.$message.error(error.response.data.message);
				}
				
			}).then(function () {});
		},
		doAdd(){
			var that = this;
			var user_condition = this.parent.$refs.user_condition
			var uri = '/user/add'
			axios.post(uri, {
				addType: 'one',
				data: that.item
			}).then(function (res) {
				console.log('res', res)
				if(user_condition){
					user_condition.search()
				}
				that.visible = false
			}).catch(function (error) {
				if(error.response && error.response.data && error.response.data.message){
					that.$message.error(error.response.data.message);
				}
				
			}).then(function () {});
		}
	}
})